package com.yhk.wuchat.common.websocket.service.adapter;

import com.yhk.wuchat.common.common.domain.enums.YesOrNoEnum;
import com.yhk.wuchat.common.user.domain.entity.User;
import com.yhk.wuchat.common.websocket.domain.enums.WSRespTypeEnum;
import com.yhk.wuchat.common.websocket.domain.vo.resp.WSBaseResp;
import com.yhk.wuchat.common.websocket.domain.vo.resp.WSBlack;
import com.yhk.wuchat.common.websocket.domain.vo.resp.WSLoginSuccess;
import com.yhk.wuchat.common.websocket.domain.vo.resp.WSLoginUrl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/4/2 17:53
 * @注释
 */

/**
 * 返回给前端的数据适配器
 */
public class WebSocketAdapter {
    /**
     * 返回登录二维码
     * @param wxMpQrCodeTicket
     * @return
     */
    public static WSBaseResp<?> buildResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        WSBaseResp<WSLoginUrl> wsBaseResp=new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        wsBaseResp.setData(new WSLoginUrl(wxMpQrCodeTicket.getUrl()));
        return wsBaseResp;
    }

    /**
     * 返回登录成功的用户信息
     * @param user
     * @param token
     * @return
     */
    public static WSBaseResp<?> buildResp(User user, String token) {
        WSBaseResp<WSLoginSuccess> wsBaseResp=new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess build = WSLoginSuccess.builder().avatar(user.getAvatar()).name(user.getName()).token(token).uid(user.getId()).build();
        wsBaseResp.setData(build);
        return wsBaseResp;
    }

    /**
     * 返回给用户等待授权的信息（在前端就是等待授权的界面，已经扫码完成了）
     * @return
     */
    public static WSBaseResp<?> buildWaitAuthorizeResp() {
        WSBaseResp<WSLoginUrl> resp=new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return resp;
    }

    /**
     * token失效的响应
     * @return
     */
    public static WSBaseResp<?> buildInvalidTokenResp() {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp();
        resp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return resp;
    }

    public static WSBaseResp<?> buildResp(User user, String token, boolean hasPower) {
        WSBaseResp<WSLoginSuccess> resp=new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess build=WSLoginSuccess.builder()
                .avatar(user.getAvatar())
                .name(user.getName())
                .token(token)
                .uid(user.getId())
                .power(hasPower? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus())
                .build();
        resp.setData(build);
        return resp;
    }

    public static WSBaseResp<?> buildBlack(User user) {
        WSBaseResp<WSBlack> resp=new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.BLACK.getType());
        WSBlack build=WSBlack.builder()
                .uid(user.getId())
                .build();
        resp.setData(build);
        return resp;
    }
}
